import hashlib
import time
# 操作html界面的
from selenium.webdriver.common.by import By

from automate_testing.tool.DButils import DButils


class  LoginOperation:
    @staticmethod
    def  login(driver,username,password):
        # 输入用户名
        driver.find_element(By.XPATH,'//*[@id="username"]').send_keys(username)
        driver.find_element(By.XPATH,'//*[@id="password"]').send_keys(password)
        driver.find_element(By.XPATH,'//*[@id="login_btn"]').click()

        time.sleep(2)

    @staticmethod
    def get_success_result(driver):
        # 成功的场合，返回标题
        return  driver.find_element(By.XPATH,'//div[@id="logo"]/a').text

    @staticmethod
    def get_failure_result(driver):
        # 失败的场合
        user_not_existent= driver.find_element(By.XPATH,'/html/body/div/div/div/form/div[1]/span[1]').text
        password_error= driver.find_element(By.XPATH,'/html/body/div/div/div/form/div[2]/span[1]').text
        return user_not_existent,password_error
    @staticmethod
    def success_sql(username,password):
        if username == 'lisi3':
            sql='insert into user(username,password,status) values(%s,%s,1)'
            DButils().update(sql,(username,hashlib.md5(password.encode('utf-8')).hexdigest()))

    @staticmethod
    def failure_sql(username):
        if username == 'lisi3':
            sql='delete from user where username=%s'
            DButils().update(sql,(username,))

